<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="表单验证" tabindex="-1"><a class="header-anchor" href="#表单验证"><span>表单验证</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#validation-quickstart">快速验证</a>
<ul>
<li><a href="#quick-defining-the-routes">定义路由</a></li>
<li><a href="#quick-creating-the-controller">创建控制器</a></li>
<li><a href="#quick-writing-the-validation-logic">编写验证逻辑</a></li>
<li><a href="#quick-displaying-the-validation-errors">显示验证错误信息</a></li>
<li><a href="#a-note-on-optional-fields">可选字段注意事项</a></li>
</ul>
</li>
<li><a href="#form-request-validation">表单请求验证</a>
<ul>
<li><a href="#creating-form-requests">创建表单请求验证</a></li>
<li><a href="#authorizing-form-requests">表单请求授权验证</a></li>
<li><a href="#customizing-the-error-messages">自定义验证错误信息</a></li>
<li><a href="#customizing-the-validation-attributes">自定义验证属性</a></li>
</ul>
</li>
<li><a href="#manually-creating-validators">手动创建验证器</a>
<ul>
<li><a href="#automatic-redirection">自动转跳</a></li>
<li><a href="#named-error-bags">命名错误包</a></li>
<li><a href="#after-validation-hook">验证后钩子</a></li>
</ul>
</li>
<li><a href="#working-with-error-messages">处理错误信息</a>
<ul>
<li><a href="#custom-error-messages">自定义验证错误信息</a></li>
</ul>
</li>
<li><a href="#available-validation-rules">可用验证规则</a></li>
<li><a href="#conditionally-adding-rules">按条件添加验证规则</a></li>
<li><a href="#validating-arrays">验证数组</a></li>
<li><a href="#custom-validation-rules">自定义验证规则</a>
<ul>
<li><a href="#using-rule-objects">使用验证规则对象</a></li>
<li><a href="#using-closures">使用闭包</a></li>
<li><a href="#using-extensions">使用扩展</a></li>
<li><a href="#implicit-extensions">隐式扩展</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 提供了几种不同的方法来验证传入应用程序的数据。默认情况下，Laravel 的控制器基类使用 <code v-pre>ValidatesRequests</code> trait，它提供了一种方便的方法去使用各种强大的验证规则来验证传入的 HTTP 请求。</p>
<h2 id="快速验证" tabindex="-1"><a class="header-anchor" href="#快速验证"><span>快速验证</span></a></h2>
<p>要了解 Laravel 强大的验证功能，让我们看一个验证表单并将错误消息显示回给用户的完整示例。</p>
<h3 id="定义路由" tabindex="-1"><a class="header-anchor" href="#定义路由"><span>定义路由</span></a></h3>
<p>首先，让我们假设在 <code v-pre>routes/web.php</code> 文件中定义了下面这些路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post/create'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PostController@create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PostController@store'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>GET</code> 路由会显示一个供用户创建一个新的博客帖子的表单，而 <code v-pre>POST</code> 路由会将新的博客文章存储在数据库中。</p>
<h3 id="创建控制器" tabindex="-1"><a class="header-anchor" href="#创建控制器"><span>创建控制器</span></a></h3>
<p>下面让我们一起来看看处理这些路由的控制器，<code v-pre>store</code> 方法暂时留空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 显示创建博客文章的表单</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post.create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 保存一篇新的博客文章</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//  验证并存储博客文章...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写验证器逻辑" tabindex="-1"><a class="header-anchor" href="#编写验证器逻辑"><span>编写验证器逻辑</span></a></h3>
<p>现在我们开始在 <code v-pre>store</code> 方法中编写逻辑来验证新的博客文章。为此，我们将使用 <code v-pre>Illuminate\Http\Request</code> 对象提供的 <code v-pre>validate</code> 方法 。如果验证通过，代码就可以正常的运行。如果验证失败，则会抛出异常，并自动将对应的错误响应返回给用户。在典型的 HTTP 请求的情况下，会生成一个重定向响应，而对于 AJAX 请求则会发送 JSON 响应。</p>
<p>让我们接着回到 <code v-pre>store</code> 方法来深入理解 <code v-pre>validate</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 保存一篇新的博客文章</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$validatedData</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 博客文章验证通过...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，我们将所需的验证规则传递至 <code v-pre>validate</code> 方法中。另外再提醒一次，如果验证失败，会自动生成一个对应的响应。如果验证通过，那我们的控制器将会继续正常运行。</p>
<p>或者，验证规则可以指定为一个数组而不是单个<code v-pre>|</code>分隔字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validatedData</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique:posts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="首次验证失败后停止运行" tabindex="-1"><a class="header-anchor" href="#首次验证失败后停止运行"><span>首次验证失败后停止运行</span></a></h4>
<p>如果你希望在某个属性第一次验证失败后停止运行验证规则，你需要附加 <code v-pre>bail</code> 规则到该属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bail|required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，如果 <code v-pre>title</code> 字段没有通过 <code v-pre>unique</code> 规则，那么程序就不会继续检查 <code v-pre>max</code> 规则。规则会按照分配的顺序来验证。</p>
<h4 id="关于数组数据的注意实现" tabindex="-1"><a class="header-anchor" href="#关于数组数据的注意实现"><span>关于数组数据的注意实现</span></a></h4>
<p>如果你的 HTTP 请求包含一个 「嵌套」 参数（即数组），那你可以在验证规则中通过 「点」 语法来指定这些参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'author.name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'author.description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="显示验证错误信息" tabindex="-1"><a class="header-anchor" href="#显示验证错误信息"><span>显示验证错误信息</span></a></h3>
<p>如果传入的请求参数未通过给定的验证规则呢？正如前面所提到的，Laravel 会自动把用户重定向到之前的位置。另外，所有的验证错误信息会被自动 <a href="https://learnku.com/docs/laravel/6.x/session#flash-data" target="_blank" rel="noopener noreferrer">存储到 session</a>。</p>
<p>重申一次，我们不必在 <code v-pre>GET</code> 路由中将错误消息显式绑定到视图。因为 Lavarel 会检查在 Session 数据中的错误信息，并自动将其绑定到视图（如果这个视图文件存在）。而其中的变量 <code v-pre>$errors</code> 是 <code v-pre>Illuminate\Support\MessageBag</code> 的一个实例。要获取关于这个对象的更多信息，请 <a href="#working-with-error-messages">查阅这个文档</a> 。</p>
<blockquote>
<p>提示：<code v-pre>$errors</code> 变量被 <code v-pre>web</code> 中间件组提供的 <code v-pre>Illuminate\View\Middleware\ShareErrorsFromSession</code> 中间件绑定到视图中。 <strong>当这个中间件被应用后，在你的视图中就可以获取到 <code v-pre>$error</code> 变量</strong> , 可以使一直假定 <code v-pre>$errors</code> 变量存在并且可以安全地使用。</p>
</blockquote>
<p>因此，在我们的例子中，当验证失败的时候，用户将会被重定向到控制器的 <code v-pre>create</code> 方法，使我们能在视图中显示错误信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token operator">/</span>resources<span class="token operator">/</span>views<span class="token operator">/</span>post<span class="token operator">/</span>create<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>h1<span class="token operator">></span>Create Post<span class="token operator">&lt;</span><span class="token operator">/</span>h1<span class="token operator">></span></span>
<span class="line"></span>
<span class="line">@<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">any</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"alert alert-danger"</span><span class="token operator">></span></span>
<span class="line">        <span class="token operator">&lt;</span>ul<span class="token operator">></span></span>
<span class="line">            @<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$error</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$error</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span></span>
<span class="line">            @<span class="token keyword">endforeach</span></span>
<span class="line">        <span class="token operator">&lt;</span><span class="token operator">/</span>ul<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line">@<span class="token keyword">endif</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> Create Post Form <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="error-指令" tabindex="-1"><a class="header-anchor" href="#error-指令"><span><code v-pre>@error</code> 指令</span></a></h4>
<p>你还可以使用 <code v-pre>@error</code> <a href="https://learnku.com/docs/laravel/6.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板</a> 指令快速检查给定属性是否存在验证错误消息。 在 <code v-pre>@error</code> 指令中，你可以输出 <code v-pre>$message</code> 变量以显示错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token operator">/</span>resources<span class="token operator">/</span>views<span class="token operator">/</span>post<span class="token operator">/</span>create<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>label <span class="token keyword">for</span><span class="token operator">=</span><span class="token string double-quoted-string">"title"</span><span class="token operator">></span>Post Title<span class="token operator">&lt;</span><span class="token operator">/</span>label<span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>input id<span class="token operator">=</span><span class="token string double-quoted-string">"title"</span> type<span class="token operator">=</span><span class="token string double-quoted-string">"text"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"@error('title') is-invalid @enderror"</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line">@<span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"alert alert-danger"</span><span class="token operator">></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$message</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line">@enderror</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="关于可选字段的注意事项" tabindex="-1"><a class="header-anchor" href="#关于可选字段的注意事项"><span>关于可选字段的注意事项</span></a></h3>
<p>默认情况下，在 Laravel 应用的全局中间件堆栈 <code v-pre>App\Http\Kernel</code> 类中包含了 <code v-pre>TrimStrings</code> 和 <code v-pre>ConvertEmptyStringsToNull</code> 中间件。因此，如果你不希望验证程序将 <code v-pre>null</code> 值视为无效的话，那就需要将「可选」的请求字段标记为 <code v-pre>nullable</code>，举个例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'publish_at'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'nullable|date'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子里，我们指定 <code v-pre>publish_at</code> 字段可以为 <code v-pre>null</code> 或者一个有效的日期格式。如果 <code v-pre>nullable</code> 的修饰词没有被添加到规则定义中，验证器会认为 <code v-pre>null</code> 是一个无效的日期格式。</p>
<h4 id="ajax-请求-验证" tabindex="-1"><a class="header-anchor" href="#ajax-请求-验证"><span>AJAX 请求 &amp; 验证</span></a></h4>
<p>在这个例子中，我们使用传统的表单将数据发送到应用程序。但实际情况中，很多程序都会使用 AJAX 来发送请求。当我们对 AJAX 的请求中使用 <code v-pre>validate</code> 方法时，Laravel 并不会生成一个重定向响应，而是会生成一个包含所有验证错误信息的 JSON 响应。这个 JSON 响应会包含一个 HTTP 状态码 422 被发送出去。</p>
<h2 id="验证表单请求" tabindex="-1"><a class="header-anchor" href="#验证表单请求"><span>验证表单请求</span></a></h2>
<h3 id="创建表单请求验证" tabindex="-1"><a class="header-anchor" href="#创建表单请求验证"><span>创建表单请求验证</span></a></h3>
<p>面对更复杂的验证情境中，你可以创建一个「表单请求」来处理更为复杂的逻辑。表单请求是包含验证逻辑的自定义请求类。可使用 Artisan 命令 <code v-pre>make:request</code> 来创建表单请求类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>request StoreBlogPost</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>新生成的类保存在 <code v-pre>app/Http/Requests</code> 目录下。如果这个目录不存在，运行 <code v-pre>make:request</code> 命令时它会被创建出来。让我们添加一些验证规则到 <code v-pre>rules</code> 方法中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取适用于请求的验证规则。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">rules</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：你可以向 rules 方法传入所需的任何依赖项。他们会自动被 Laravel 提供的 <a href="https://learnku.com/docs/laravel/6.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 自动解析。</p>
</blockquote>
<p>所以，验证规则是如何运行的呢？你所需要做的就是在控制器方法中类型提示传入的请求。在调用控制器方法之前验证传入的表单请求，这意味着你不需要在控制器中写任何验证逻辑：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 存储传入的博客文章。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name">StoreBlogPost</span>  <span class="token parameter">$request</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">StoreBlogPost</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 传入的请求通过验证...</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 获取通过验证的数据...</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果验证失败，就会生成一个让用户返回到先前的位置的重定向响应。这些错误也会被闪存到 <code v-pre>session</code> 中，以便这些错误都可以在页面中显示出来。如果传入的请求是 AJAX，会向用户返回具有 422 状态代码和验证错误信息的 JSON 数据的 HTTP 响应。</p>
<h4 id="添加表单请求后钩子" tabindex="-1"><a class="header-anchor" href="#添加表单请求后钩子"><span>添加表单请求后钩子</span></a></h4>
<p>如果你想在表单请求「之后」添加钩子，可以使用 <code v-pre>withValidator</code> 方法。这个方法接收一个完整的验证构造器，允许你在验证结果返回之前调用任何方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 配置验证器实例。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span>  <span class="token parameter">$validator</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">withValidator</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something is wrong with this field!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="表单请求授权验证" tabindex="-1"><a class="header-anchor" href="#表单请求授权验证"><span>表单请求授权验证</span></a></h3>
<p>表单请求类内也包含了 <code v-pre>authorize</code> 方法。在这个方法中，你可以检查经过身份验证的用户确定其是否具有更新给定资源的权限。比方说，你可以判断用户是否拥有更新文章评论的权限：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 判断用户是否有权限做出此请求。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">authorize</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$comment</span> <span class="token operator">=</span> <span class="token class-name static-context">Comment</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$comment</span> <span class="token operator">&amp;&amp;</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$comment</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于所有的表单请求都是继承了 Laravel 中的请求基类，所以我们可以使用 <code v-pre>user</code> 方法去获取当前认证登录的用户。同时请注意上述例子中对 <code v-pre>route</code> 方法的调用。这个方法允许你在被调用的路由上获取其定义的 URI 参数，譬如下面例子中的 <code v-pre>{comment}</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment/{comment}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果 <code v-pre>authorize</code> 方法返回 <code v-pre>false</code>，则会自动返回一个包含 403 状态码的 HTTP 响应，也不会运行控制器的方法。</p>
<p>如果你打算在应用程序的其它部分处理授权逻辑，只需从 <code v-pre>authorize</code> 方法返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 判断用户是否有权限进行此请求。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">authorize</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：你可以向 authorize 方法传入所需的任何依赖项。他们会自动被 Laravel 提供的 <a href="https://learnku.com/docs/laravel/6.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 自动解析。.</p>
</blockquote>
<h3 id="自定义错误消息" tabindex="-1"><a class="header-anchor" href="#自定义错误消息"><span>自定义错误消息</span></a></h3>
<p>你可以通过重写表单请求的 <code v-pre>messages</code> 方法来自定义错误消息。此方法应返回属性 / 规则对及其对应错误消息的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取已定义验证规则的错误消息。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">messages</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'A title is required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body.required'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'A message is required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义验证属性" tabindex="-1"><a class="header-anchor" href="#自定义验证属性"><span>自定义验证属性</span></a></h3>
<p>如果你希望将验证消息的 <code v-pre>:attribute</code> 部分替换为自定义属性名称，则可以重写 <code v-pre>attributes</code> 方法来指定自定义名称。此方法应返回属性 / 名称对的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取验证错误的自定义属性。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="手动创建验证器" tabindex="-1"><a class="header-anchor" href="#手动创建验证器"><span>手动创建验证器</span></a></h2>
<p>如果你不想在请求上使用 <code v-pre>validate</code> 方法，你可以通过 <code v-pre>Validator</code> <a href="https://learnku.com/docs/laravel/6.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 手动创建一个验证器示例。用 <code v-pre>Validator</code> <a href="https://learnku.com/docs/laravel/6.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 上的 <code v-pre>make</code> 方法创建一个验证器示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 保存一篇新的博客文章。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post/create'</span><span class="token punctuation">)</span></span>
<span class="line">                        <span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span></span>
<span class="line">                        <span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 保存博客文章…</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传给 <code v-pre>make</code> 方法的第一个参数是需要验证的数据。第二个参数则是该数据的验证规则。</p>
<p>如果验证失败，则可以使用 <code v-pre>withErrors</code> 方法把错误消息闪存到 Session 。使用这个方法进行重定向后， <code v-pre>$errors</code> 变量会自动和视图共享，你可以把这些消息显示给用户。<code v-pre>withErrors</code> 方法接收验证器、<code v-pre>MessageBag</code> 或 PHP <code v-pre>Array</code> 。</p>
<h3 id="自动重定向" tabindex="-1"><a class="header-anchor" href="#自动重定向"><span>自动重定向</span></a></h3>
<p>如果你想手动创建验证器实例，又想使用 <code v-pre>validates</code> 方法提供的自动重定向，那么你可以在现有的验证器示例上调用 <code v-pre>validate</code> 方法。如果验证失败，用户将会自动重定向。在 AJAX 请求中，则会返回 JSON 格式的响应。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="命名错误包" tabindex="-1"><a class="header-anchor" href="#命名错误包"><span>命名错误包</span></a></h3>
<p>如果你一个页面中有多个表单，你可以通过命名错误包来检索特定表单的错误消息。只需给 <code v-pre>withErrors</code> 方法传递一个名字作为第二个参数:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'register'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>然后你就可以从 <code v-pre>$errors</code> 变量中获取指定表单的错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$errors</span><span class="token operator">-></span><span class="token property">login</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="验证后钩子" tabindex="-1"><a class="header-anchor" href="#验证后钩子"><span>验证后钩子</span></a></h3>
<p>验证器还允许附加回调并在验证完成后执行，以便你进行下一步的验证，甚至在消息集合中添加更多的错误消息。使用它只需在验证实例上使用 <code v-pre>after</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something is wrong with this field!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="处理错误消息" tabindex="-1"><a class="header-anchor" href="#处理错误消息"><span>处理错误消息</span></a></h2>
<p>通过 <code v-pre>Validator</code> 实例调用 <code v-pre>errors</code> 方法，会返回 <code v-pre>Illuminate\Support\MessageBag</code> 实例，它拥有各种方便的方法处理错误信息。自动提供给所有视图的 <code v-pre>$errors</code> 变量，也是 <code v-pre>MessageBag</code> 类的一个实例。</p>
<h4 id="查看特定字段的第一个错误信息" tabindex="-1"><a class="header-anchor" href="#查看特定字段的第一个错误信息"><span>查看特定字段的第一个错误信息</span></a></h4>
<p>要查看特定字段的第一个错误消息，可以使用 <code v-pre>first</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$errors</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="查看特定字段的所有错误消息" tabindex="-1"><a class="header-anchor" href="#查看特定字段的所有错误消息"><span>查看特定字段的所有错误消息</span></a></h4>
<p>如果你需要获取指定字段的所有错误信息的数组，则可以使用 <code v-pre>get</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果要验证表单的数组字段，你可以使用 <code v-pre>*</code> 来获取每个数组元素的所有错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'attachments.*'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="查看所有字段的所有错误消息" tabindex="-1"><a class="header-anchor" href="#查看所有字段的所有错误消息"><span>查看所有字段的所有错误消息</span></a></h4>
<p>如果你想要得到所有字段的所有错误消息，可以使用 <code v-pre>all</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="判断特定字段是否含有错误消息" tabindex="-1"><a class="header-anchor" href="#判断特定字段是否含有错误消息"><span>判断特定字段是否含有错误消息</span></a></h4>
<p><code v-pre>has</code> 方法可以被用来判断指定字段是否存在错误信息:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义错误消息-1" tabindex="-1"><a class="header-anchor" href="#自定义错误消息-1"><span>自定义错误消息</span></a></h3>
<p>如果有需要，你也可以使用自定义错误信息代替默认值进行验证。有几种方法可以指定自定义信息。首先，你可以将自定义信息作为第三个参数传递给 <code v-pre>Validator::make</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute field is required.'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中， <code v-pre>:attribute</code> 占位符会被验证字段的实际名称替换。除此之外，你还可以在验证消息中使用其它占位符。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'same'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute and :other must match.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'size'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute must be exactly :size.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'between'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute value :input is not between :min - :max.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'in'</span>      <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute must be one of the following types: :values'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="为给定属性指定自定义信息" tabindex="-1"><a class="header-anchor" href="#为给定属性指定自定义信息"><span>为给定属性指定自定义信息</span></a></h4>
<p>有时候你可能只想为特定的字段自定义错误信息。只需在属性名称后使用「点」来指定验证的规则即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'We need to know your e-mail address!'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在php文件中指定自定义信息" tabindex="-1"><a class="header-anchor" href="#在php文件中指定自定义信息"><span>在PHP文件中指定自定义信息</span></a></h4>
<p>在大多数情况下，您可能会在文件中指定自定义信息，而不是直接将它们传递给 <code v-pre>Validator</code> 。为此，需要把你的信息放置于 <code v-pre>resources/lang/xx/validation.php</code> 语言文件内的 <code v-pre>custom</code> 数组中。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'custom'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'We need to know your e-mail address!'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在php文件中指定自定义属性" tabindex="-1"><a class="header-anchor" href="#在php文件中指定自定义属性"><span>在PHP文件中指定自定义属性</span></a></h4>
<p>如果你希望将验证信息的 <code v-pre>:attribute</code> 部分替换为自定义属性名称，你可以在 <code v-pre>resources/lang/xx/validation.php</code> 语言文件的 <code v-pre>attributes</code> 数组中指定自定义名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'attributes'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在php文件中指定自定义值" tabindex="-1"><a class="header-anchor" href="#在php文件中指定自定义值"><span>在PHP文件中指定自定义值</span></a></h4>
<p>有时您可能需要将验证信息的 <code v-pre>:value</code> 替换为自定义的表示形式。例如，指定 <code v-pre>payment_type</code> 的值为 <code v-pre>cc</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'credit_card_number'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required_if:payment_type,cc'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果此验证规则失败，将生成以下错误信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">The credit card number field is required when payment type is cc<span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您可以通过在 <code v-pre>validation</code> 语言文件中定义 <code v-pre>values</code> 数组指定自定义值表示形式，而不是将 <code v-pre>cc</code> 显示为 <code v-pre>payment_type</code> 的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'values'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'payment_type'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'cc'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'credit card'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果验证失败，将生成以下信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">The credit card number field is required when payment type is credit card<span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="可用验证规则" tabindex="-1"><a class="header-anchor" href="#可用验证规则"><span>可用验证规则</span></a></h2>
<p>以下是所有可用验证规则及其功能的列表：</p>
<h4 id="accepted" tabindex="-1"><a class="header-anchor" href="#accepted"><span>accepted</span></a></h4>
<p>验证字段必须是 yes， on， 1，或 true。这在确认「服务条款」是否同意时相当有用。</p>
<h4 id="active-url" tabindex="-1"><a class="header-anchor" href="#active-url"><span>active_url</span></a></h4>
<p>根据 PHP 函数 <code v-pre>dns_get_record</code> ，验证字段必须具有有效的 A 或 AAAA 记录。</p>
<h4 id="after-date" tabindex="-1"><a class="header-anchor" href="#after-date"><span>after:<em>date</em></span></a></h4>
<p>验证字段必须是给定日期之后的值。日期值将传递到 PHP 函数 <code v-pre>strtotime</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'start_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|date|after:tomorrow'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您可以指定另一个要与日期进行比较的字段，而不是传递要由 <code v-pre>strtotime</code> 处理的日期字符串:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'finish_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|date|after:start_date'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="after-or-equal-date" tabindex="-1"><a class="header-anchor" href="#after-or-equal-date"><span>after_or_equal:<em>date</em></span></a></h4>
<p>验证字段必须是在给定日期之后或与此日期相同的值。更多信息，请参阅 <a href="#rule-after">after</a> 。</p>
<h4 id="alpha" tabindex="-1"><a class="header-anchor" href="#alpha"><span>alpha</span></a></h4>
<p>验证字段必须完全由字母构成。</p>
<h4 id="alpha-dash" tabindex="-1"><a class="header-anchor" href="#alpha-dash"><span>alpha_dash</span></a></h4>
<p>验证字段可能包含字母、数字，以及破折号 (-) 和下划线 ( _ )。</p>
<h4 id="alpha-num" tabindex="-1"><a class="header-anchor" href="#alpha-num"><span>alpha_num</span></a></h4>
<p>验证字段必须是完全是字母、数字。</p>
<h4 id="array" tabindex="-1"><a class="header-anchor" href="#array"><span>array</span></a></h4>
<p>验证的字段必须是一个 PHP 数组。</p>
<h4 id="bail" tabindex="-1"><a class="header-anchor" href="#bail"><span>bail</span></a></h4>
<p>在第一次验证失败后停止运行验证规则。</p>
<h4 id="before-date" tabindex="-1"><a class="header-anchor" href="#before-date"><span>before:<em>date</em></span></a></h4>
<p>正在验证的字段必须是给定日期之前的值。这个日期将传递到 PHP 的 <code v-pre>strtotime</code> 函数中。此外，与 <a href="#rule-after"><code v-pre>after</code></a> 规则一样，另一个正在验证的字段可以作为 <code v-pre>date</code> 的值。</p>
<h4 id="before-or-equal-date" tabindex="-1"><a class="header-anchor" href="#before-or-equal-date"><span>before_or_equal:<em>date</em></span></a></h4>
<p>验证字段必须是在给定日期之前或与之相同的日期。这个日期值将会被传递给 PHP 的 <code v-pre>strtotime</code> 函数来计算。</p>
<h4 id="between-min-max" tabindex="-1"><a class="header-anchor" href="#between-min-max"><span>between:<em>min</em>,<em>max</em></span></a></h4>
<p>验证字段的大小必须在给定的 <em>min</em> 和 <em>max</em> 之间。字符串、数字、数组和文件的计算方式都使用 <code v-pre>size</code> 方法。</p>
<h4 id="boolean" tabindex="-1"><a class="header-anchor" href="#boolean"><span>boolean</span></a></h4>
<p>验证的字段必须可以转换为 Boolean 类型。 可接受的输入为 <code v-pre>true</code> ， <code v-pre>false</code> ， <code v-pre>1</code> ， <code v-pre>0</code> ， <code v-pre>&quot;1&quot;</code> 和 <code v-pre>&quot;0&quot;</code> 。</p>
<h4 id="confirmed" tabindex="-1"><a class="header-anchor" href="#confirmed"><span>confirmed</span></a></h4>
<p>验证字段必须具有匹配字段 <code v-pre>foo_confirmation</code> 。例如，验证字段为 <code v-pre>password</code> ，输入中必须存在与之匹配的 <code v-pre>password_confirmation</code> 字段。</p>
<h4 id="date" tabindex="-1"><a class="header-anchor" href="#date"><span>date</span></a></h4>
<p>根据 PHP <code v-pre>strtotime</code> 函数，验证的字段必须是有效的日期。</p>
<h4 id="date-equals-date" tabindex="-1"><a class="header-anchor" href="#date-equals-date"><span>date_equals:<em>date</em></span></a></h4>
<p>验证字段必须等于给定日期。日期将传递到 PHP <code v-pre>strtotime</code> 函数。</p>
<h4 id="date-format-format" tabindex="-1"><a class="header-anchor" href="#date-format-format"><span>date_format:<em>format</em></span></a></h4>
<p>验证字段必须匹配给定的日期格式。当验证某个字段的时候，你应该 <strong>只使用</strong> <code v-pre>date</code> 或者 <code v-pre>date_format</code> ，而不是同时使用。此验证规则支持 PHP 所有的 <a href="https://www.php.net/manual/es/class.datetime.php" target="_blank" rel="noopener noreferrer">DateTime</a> 类。</p>
<h4 id="different-field" tabindex="-1"><a class="header-anchor" href="#different-field"><span>different:<em>field</em></span></a></h4>
<p>验证的字段值必须与字段 <em>field</em> 的值不同。</p>
<h4 id="digits-value" tabindex="-1"><a class="header-anchor" href="#digits-value"><span>digits:<em>value</em></span></a></h4>
<p>验证的字段必须为 <em>numeric</em> ，并且必须具有确切长度 <em>value</em> 。</p>
<h4 id="digits-between-min-max" tabindex="-1"><a class="header-anchor" href="#digits-between-min-max"><span>digits_between:<em>min</em>,<em>max</em></span></a></h4>
<p>验证字段的长度必须在给定的 <em>min</em> 和 <em>max</em> 之间。</p>
<h4 id="dimensions" tabindex="-1"><a class="header-anchor" href="#dimensions"><span>dimensions</span></a></h4>
<p>验证的文件必须是图片并且图片比例必须符合规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dimensions:min_width=100,min_height=200'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>可用的规则为:<em>min_width</em>, <em>max_width</em>, <em>min_height</em>, <em>max_height</em>, <em>width</em>, <em>height</em>, <em>ratio</em> 。</p>
<p><em>ratio</em> 约束应该表示为宽度除以高度。 这可以通过像 <code v-pre>3/2</code> 这样的语句或像 <code v-pre>1.5</code> 这样的 float 来指定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dimensions:ratio=3/2'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>由于此规则需要多个参数，因此你可以 <code v-pre>Rule::dimensions</code> 方法来构造可读性高的规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxWidth</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxHeight</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ratio</span><span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="distinct" tabindex="-1"><a class="header-anchor" href="#distinct"><span>distinct</span></a></h4>
<p>验证数组时，指定的字段不能有任何重复值。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="email" tabindex="-1"><a class="header-anchor" href="#email"><span>email</span></a></h4>
<p>验证的字段必须符合 e-mail 地址格式。当前版本，此种验证规则由<a href="https://github.com/egulias/EmailValidator" target="_blank" rel="noopener noreferrer"><code v-pre>egulias/email-validator</code></a> 提供支持。默认使用<code v-pre>RFCValidation</code>验证样式，但你也可以使其他验证样式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email:rfc,dns'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>例子使用 <code v-pre>RFCValidation</code> 和 <code v-pre>DNSCheckValidation</code> 验证样式。所有可用验证样式列表：</p>
<ul>
<li><code v-pre>rfc</code>: <code v-pre>RFCValidation</code></li>
<li><code v-pre>strict</code>: <code v-pre>NoRFCWarningsValidation</code></li>
<li><code v-pre>dns</code>: <code v-pre>DNSCheckValidation</code></li>
<li><code v-pre>spoof</code>: <code v-pre>SpoofCheckValidation</code></li>
<li><code v-pre>filter</code>: <code v-pre>FilterEmailValidation</code></li>
</ul>
<p>当下版本<code v-pre>filter</code>验证规则使用PHP的<code v-pre>filter_var</code>方法进行验证，在5.8版本接入 Laravel 。</p>
<h4 id="ends-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#ends-with-foo-bar"><span>ends_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段必须以给定的值之一结尾。</p>
<h4 id="exists-table-column" tabindex="-1"><a class="header-anchor" href="#exists-table-column"><span>exists:<em>table</em>,<em>column</em></span></a></h4>
<p>验证的字段必须存在于给定的数据库表中。</p>
<h4 id="exists-规则的基本用法" tabindex="-1"><a class="header-anchor" href="#exists-规则的基本用法"><span>Exists 规则的基本用法</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:states'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果未指定 <code v-pre>column</code>选项，则将使用字段名称。</p>
<h4 id="指定列名" tabindex="-1"><a class="header-anchor" href="#指定列名"><span>指定列名</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:states,abbreviation'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你需要指定 <code v-pre>exists</code> 方法用来查询的数据库。你可以通过使用「点」语法将数据库的名称添加到数据表前面来实现这个目的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:connection.staff,email'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果要自定义验证规则执行的查询，可以使用<code v-pre>Rule</code> 类来定义规则。在这个例子中，我们使用数组指定验证规则，而不是使用 <code v-pre>|</code> 字符来分隔它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'staff'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="file" tabindex="-1"><a class="header-anchor" href="#file"><span>file</span></a></h4>
<p>验证的字段必须是成功上传的文件。</p>
<h4 id="filled" tabindex="-1"><a class="header-anchor" href="#filled"><span>filled</span></a></h4>
<p>验证的字段在存在时不能为空。</p>
<h4 id="gt-field" tabindex="-1"><a class="header-anchor" href="#gt-field"><span>gt:<em>field</em></span></a></h4>
<p>验证字段必须大于给定的 field。两个字段必须是相同的类型。字符串、数字、数组和文件都使用 size 进行相同的评估。</p>
<h4 id="gte-field" tabindex="-1"><a class="header-anchor" href="#gte-field"><span>gte:<em>field</em></span></a></h4>
<p>验证字段必须大于或等于给定的_field_。两个字段必须是相同的类型。字符串、数字、数组和文件都使用 <code v-pre>size</code> 进行相同的评估。</p>
<h4 id="image" tabindex="-1"><a class="header-anchor" href="#image"><span>image</span></a></h4>
<p>验证的文件必须是图片 (jpeg, png, bmp, gif, svg, or webp)</p>
<h4 id="in-foo-bar" tabindex="-1"><a class="header-anchor" href="#in-foo-bar"><span>in:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证字段必须包含在给定的值列表中。由于此规则通常要求您 <code v-pre>implode</code> 数组，因此可以使用 <code v-pre>Rule :: in</code> 方法流畅地构造规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'zones'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">in</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first-zone'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second-zone'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="in-array-anotherfield" tabindex="-1"><a class="header-anchor" href="#in-array-anotherfield"><span>in_array:<em>anotherfield</em>.*</span></a></h4>
<p>验证的字段必须存在于另一个字段 <em>anotherfield</em> 的值中。</p>
<h4 id="integer" tabindex="-1"><a class="header-anchor" href="#integer"><span>integer</span></a></h4>
<p>验证的字段必须是整数。</p>
<blockquote>
<p>{注} 此种验证规则不是验证数据是“integer”类型，仅验证字符串或数值包含一个integer.</p>
</blockquote>
<h4 id="ip" tabindex="-1"><a class="header-anchor" href="#ip"><span>ip</span></a></h4>
<p>验证的字段必须是 IP 地址。</p>
<h4 id="ipv4" tabindex="-1"><a class="header-anchor" href="#ipv4"><span>ipv4</span></a></h4>
<p>验证的字段必须是 IPv4 地址。</p>
<h4 id="ipv6" tabindex="-1"><a class="header-anchor" href="#ipv6"><span>ipv6</span></a></h4>
<p>验证的字段必须是 IPv6 地址。</p>
<h4 id="json" tabindex="-1"><a class="header-anchor" href="#json"><span>json</span></a></h4>
<p>验证的字段必须是有效的 JSON 字符串。</p>
<h4 id="lt-field" tabindex="-1"><a class="header-anchor" href="#lt-field"><span>lt:<em>field</em></span></a></h4>
<p>验证的字段必须小于给定的<em>field</em>.。这两个字段必须是相同的类型。字符串、数值、数组和文件大小的计算方式与 <code v-pre>size</code> 方法进行评估。</p>
<h4 id="lte-field" tabindex="-1"><a class="header-anchor" href="#lte-field"><span>lte:<em>field</em></span></a></h4>
<p>验证中的字段必须小于或等于给定的 _字段 _。这两个字段必须是相同的类型。字符串、数值、数组和文件大小的计算方式与<code v-pre>size</code> 方法进行评估。</p>
<h4 id="max-value" tabindex="-1"><a class="header-anchor" href="#max-value"><span>max:<em>value</em></span></a></h4>
<p>验证中的字段必须小于或等于 _value_。字符串、数字、数组或是文件大小的计算方式都用 <a href="#rule-size"><code v-pre>size</code></a> 规则。</p>
<h4 id="mimetypes-text-plain" tabindex="-1"><a class="header-anchor" href="#mimetypes-text-plain"><span>mimetypes:<em>text/plain</em>,…</span></a></h4>
<p>验证的文件必须具备与列出的其中一个扩展相匹配的 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'video'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mimetypes:video/avi,video/mpeg,video/quicktime'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为了确定上传文件的 MIME，框架将会读取文件，然后自动推测文件 MIME 类型，这可能与客户端提供的 MIME 类型不一致 。</p>
<h4 id="mimes-foo-bar" tabindex="-1"><a class="header-anchor" href="#mimes-foo-bar"><span>mimes:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的文件必须具有与列出的其中一个扩展名相对应的 MIME 类型。</p>
<h4 id="mime-规则基本用法" tabindex="-1"><a class="header-anchor" href="#mime-规则基本用法"><span>MIME 规则基本用法</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'photo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mimes:jpeg,bmp,png'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>即使你可能只需要验证指定扩展名, 但此规则实际上会去验证文件的 MIME 类型，其通过读取文件内容来推测它的 MIME 类型。</p>
<p>可以在以下链接中找到完整的 MIME 类型列表及相对应的扩展名： <a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types" target="_blank" rel="noopener noreferrer">svn.apache.org/repos/asf/httpd/htt...</a></p>
<h4 id="min-value" tabindex="-1"><a class="header-anchor" href="#min-value"><span>min:<em>value</em></span></a></h4>
<p>验证字段必须具有最小值。 字符串，数值，数组，文件大小的计算方式都与 <a href="#rule-size"><code v-pre>size</code></a> 规则一致.</p>
<h4 id="not-in-foo-bar" tabindex="-1"><a class="header-anchor" href="#not-in-foo-bar"><span>not_in:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证字段不能包含在给定的值的列表中。 使用 <code v-pre>Rule::notIn</code> 方法可以更流畅的构建这个规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'toppings'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">notIn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sprinkles'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cherries'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="not-regex-pattern" tabindex="-1"><a class="header-anchor" href="#not-regex-pattern"><span>not_regex:<em>pattern</em></span></a></h4>
<p>验证字段必须与给定的正则表达式不匹配。</p>
<p>验证时，这个规则使用 PHP <code v-pre>preg_match</code> 函数。指定的模式应遵循 <code v-pre>preg_match</code> 所需的相同格式，也包括有效的分隔符。 例如： <code v-pre>'email' =&gt; 'not_regex:/^.+$/i'</code>.</p>
<p><strong>注意：</strong> 当使用 <code v-pre>regex</code> / <code v-pre>not_regex</code> 模式时， 可能需要在数组中指定规则，而不是使用 <code v-pre>|</code> 分隔符 ，特别是在正则表达式包含 <code v-pre>|</code> 字符 的情况下。</p>
<h4 id="nullable" tabindex="-1"><a class="header-anchor" href="#nullable"><span>nullable</span></a></h4>
<p>验证字段可以为 <code v-pre>null</code>。这在验证基本数据类型时特别有用，例如可以包含空值的字符串和整数。</p>
<h4 id="numeric" tabindex="-1"><a class="header-anchor" href="#numeric"><span>numeric</span></a></h4>
<p>验证字段必须为数值。</p>
<h4 id="password" tabindex="-1"><a class="header-anchor" href="#password"><span>password</span></a></h4>
<p>验证中的字段必须与经过身份验证的用户的密码匹配。您可以使用规则的第一个参数来指定身份验证的「看守器」:</p>
<p><code v-pre>'password' =&gt; 'password:api'</code></p>
<h4 id="present" tabindex="-1"><a class="header-anchor" href="#present"><span>present</span></a></h4>
<p>验证字段必须存在于输入数据中，但可以为空。</p>
<h4 id="regex-pattern" tabindex="-1"><a class="header-anchor" href="#regex-pattern"><span>regex:<em>pattern</em></span></a></h4>
<p>验证字段必须与给定的正则表达式匹配。</p>
<p>验证时，这个规则使用 PHP 的 <code v-pre>preg_match</code> 函数。 指定的模式应遵循 <code v-pre>preg_match</code> 所需的相同格式，也包括有效的分隔符。 例如： <code v-pre>'email' =&gt; 'not_regex:/^.+$/i'</code> 。</p>
<p><strong>注意：</strong> 当使用 <code v-pre>regex</code> / <code v-pre>not_regex</code> 模式时， 可能需要在数组中指定规则，而不是使用 <code v-pre>|</code> 分隔符 ，特别是在正则表达式包含 <code v-pre>|</code> 字符 的情况下。</p>
<h4 id="required" tabindex="-1"><a class="header-anchor" href="#required"><span>required</span></a></h4>
<p>验证的字段必须存在于输入数据中，而不是空。如果满足以下条件之一，则字段被视为「空」：</p>
<ul>
<li>值为 <code v-pre>null</code> 。</li>
<li>值为空字符串。</li>
<li>值为空数组或空 <code v-pre>Countable</code> 对象。</li>
<li>值为无路径的上传文件。</li>
</ul>
<h4 id="required-if-anotherfield-value1-value2-value3" tabindex="-1"><a class="header-anchor" href="#required-if-anotherfield-value1-value2-value3"><span>required_if:<em>anotherfield</em>,<em>value1</em>,<em>value2</em>,<em>value3</em>…</span></a></h4>
<p>如果其它字段 <code v-pre>anotherfield</code> 为任一值 <code v-pre>value1</code> 或 <code v-pre>value2</code> 或 <code v-pre>value3</code> 等（也可只有一个 <code v-pre>value1</code>） ，则此验证字段必须存在且不为空。</p>
<p>如果您需要构造更复杂的条件 <code v-pre>required_if</code> 规则， 您可以使用 <code v-pre>Rule::requiredIf</code> 方法。这个方法可以接受一个布尔值或是一个闭包函数，当传递闭包函数时，闭包函数应该返回 <code v-pre>true</code> 或 <code v-pre>false</code> ，以表明是否需要验证此字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">requiredIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">requiredIf</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="required-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#required-unless-anotherfield-value"><span>required_unless:<em>anotherfield</em>,<em>value</em>,…</span></a></h4>
<p>如果其它字段 <code v-pre>anotherfield</code> 不等于任一值 <code v-pre>value</code> ，则此验证字段必须存在且不为空。</p>
<h4 id="required-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-with-foo-bar"><span>required_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>在其他任一指定字段出现时，验证的字段才必须存在且不为空。</p>
<h4 id="required-with-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-with-all-foo-bar"><span>required_with_all:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>只有在其他指定字段全部出现时，验证的字段才必须存在且不为空。</p>
<h4 id="required-without-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-without-foo-bar"><span>required_without:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>在其他指定任一字段不出现时，验证的字段才必须存在且不为空。</p>
<h4 id="required-without-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-without-all-foo-bar"><span>required_without_all:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>只有在其他指定字段全部不出现时，验证的字段才必须存在且不为空。</p>
<h4 id="same-field" tabindex="-1"><a class="header-anchor" href="#same-field"><span>same:<em>field</em></span></a></h4>
<p>验证字段必须与给定字段相匹配。</p>
<h4 id="size-value" tabindex="-1"><a class="header-anchor" href="#size-value"><span>size:<em>value</em></span></a></h4>
<p>验证字段必须与给定值的大小一致。对于字符串，value 对应字符数。对于数字，value 对应给定的整数值。对于数组，size 对应数组的 <code v-pre>count</code> 值。对于文件，size 对应文件大小（单位 kb）。</p>
<h4 id="starts-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#starts-with-foo-bar"><span>starts_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证字段必须以给定值之一开头。</p>
<h4 id="string" tabindex="-1"><a class="header-anchor" href="#string"><span>string</span></a></h4>
<p>验证字段必须是一个字符串。如果允许这个字段为 <code v-pre>null</code>，需要给这个字段分配 <code v-pre>nullable</code> 规则。</p>
<h4 id="timezone" tabindex="-1"><a class="header-anchor" href="#timezone"><span>timezone</span></a></h4>
<p>验证字段必须为符合 PHP 函数 <code v-pre>timezone_identifiers_list</code> 所定义的有效时区标识。</p>
<h4 id="unique-table-column-except-idcolumn" tabindex="-1"><a class="header-anchor" href="#unique-table-column-except-idcolumn"><span>unique:<em>table</em>,<em>column</em>,<em>except</em>,<em>idColumn</em></span></a></h4>
<p>验证字段在给定的数据库表中必须是唯一的。</p>
<p><strong>指定自定义列名：</strong></p>
<p><code v-pre>column</code> 选项可用于指定相应数据库列的字段。 如果未指定 <code v-pre>column</code> 选项，则使用字段本身名称。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:users,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>自定义数据库连接</strong></p>
<p>有时，你可能需要为验证器创建的数据库查询设置自定义连接。上面的例子中，将 <code v-pre>unique:users</code> 设置为验证规则，等于使用默认数据库连接来查询数据库。如果要对其进行修改，请使用「点」语法来指定连接和表名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:connection.users,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>强迫 Unique 规则忽略指定 ID ：</strong></p>
<p>有时，你可能希望在进行字段唯一性验证时忽略指定 ID 。例如， 在「更新个人资料」页面会包含用户名、邮箱和地点。这时你会想要验证更新的 E-mail 值是否唯一。如果用户仅更改了用户名字段而没有改 E-mail 字段，就不需要抛出验证错误，因为此用户已经是这个 E-mail 的拥有者了。</p>
<p>使用 Rule 类定义规则来指示验证器忽略用户的 ID 。这个例子中通过数组来指定验证规则，而不是使用 <code v-pre>|</code> 字符来分隔：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{备注} 您永远不应该将任何用户控制的请求输入传递给 <code v-pre>ignore</code> 方法。您应该只通过 Eloquent 模型的实例来传递系统生成的唯一 ID ，例如自动递增 ID 或 UUID 。 否则，您的应用程序将更容易受到 SQL 注入攻击。</p>
</blockquote>
<p>您可以传递整个模型实例，而不是将模型实例的主键值 传递给 <code v-pre>ignore</code> 方法。 Laravel 将自动从模型实例中获取主键值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您的数据表使用的主键名称不是 <code v-pre>id</code> ，那就在调用 <code v-pre>ignore</code> 方法时指定字段的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下， <code v-pre>unique</code> 规则将检查与要验证字段名称相匹配的列的唯一性。 但是，您可以传递一个不同的列名作为 <code v-pre>unique</code> 方法的第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email_address'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>增加额外的 Where 语句：</strong></p>
<p>您也可以通过 <code v-pre>where</code> 方法指定额外的查询条件。例如， 我们添加 <code v-pre>account_id</code> 为 <code v-pre>1</code> 的约束：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="url" tabindex="-1"><a class="header-anchor" href="#url"><span>url</span></a></h4>
<p>验证的字段必须是有效的 URL。</p>
<h4 id="uuid" tabindex="-1"><a class="header-anchor" href="#uuid"><span>uuid</span></a></h4>
<p>验证字段必须是有效的 RFC 4122（版本 1,3,4 或 5）通用唯一标识符（UUID）。</p>
<h2 id="按条件增加规则" tabindex="-1"><a class="header-anchor" href="#按条件增加规则"><span>按条件增加规则</span></a></h2>
<h4 id="存在时则验证" tabindex="-1"><a class="header-anchor" href="#存在时则验证"><span>存在时则验证</span></a></h4>
<p>在某些情况下，你可能希望将要验证的字段存在于输入数组中时，才对该字段执行验证。可以在规则列表中增加 <code v-pre>sometimes</code> 来实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$v</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'sometimes|required|email'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的例子中， <code v-pre>email</code> 字段只有在 <code v-pre>$data</code> 数组中存在时才会被验证。</p>
<blockquote>
<p>Tip：如果你尝试验证应该始终存在但可能为空的字段，请查阅 <a href="#a-note-on-optional-fields">可选字段的注意事项</a></p>
</blockquote>
<h4 id="复杂的条件验证" tabindex="-1"><a class="header-anchor" href="#复杂的条件验证"><span>复杂的条件验证</span></a></h4>
<p>有时候你可能需要增加基于更复杂的条件逻辑的验证规则。例如，你可以希望某个指定字段在另一个字段的值超过 100 时才为必填。或者当某个指定字段存在时，另外两个字段才能具有给定的值。增加这样的验证条件并不难。首先，使用 静态规则 创建一个 <code v-pre>Validator</code> 实例:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$v</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'games'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|numeric'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>假设我们有一个专为游戏收藏家所设计的网页应用程序。如果游戏收藏家收藏超过一百款游戏，我们会希望他们来说明下为什么他们会拥有这么多游戏。比如说他们有可能经营了一家游戏分销商店，或者只是为了享受收集的乐趣。为了在特定条件下加入此验证需求，可以在 <code v-pre>Validator</code> 实例中使用 <code v-pre>sometimes</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$v</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'reason'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required|max:500'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传入 <code v-pre>sometimes</code> 方法的第一个参数是要用来验证的字段名称。第二个参数是我们想使用的验证规则。 <code v-pre>闭包</code> 作为第三个参数传入，如果其返回 <code v-pre>true</code> ， 则额外的规则就会被加入。这个方法可以轻松地创建复杂的条件验证。你甚至可以一次对多个字段增加条件验证：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$v</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'reason'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cost'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：传入 <code v-pre>闭包</code> 的 <code v-pre>$input</code> 参数是 <code v-pre>Illuminate\Support\Fluent</code> 的一个实例，可用来访问你的输入或文件对象。</p>
</blockquote>
<h2 id="验证数组" tabindex="-1"><a class="header-anchor" href="#验证数组"><span>验证数组</span></a></h2>
<p>验证表单的输入为数组的字段也不难。你可以使用 「点」方法来验证数组中的属性。例如，如果传入的 HTTP 请求中包含 <code v-pre>photos[profile]</code> 字段， 可以如下验证：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.profile'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|image'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以验证数组中的每个元素。例如，要验证指定数组输入字段中的每一个 email 是唯一的，可以这么做：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email|unique:users'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.first_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required_with:person.*.last_name'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同理，你可以在语言文件定义验证信息时使用 <code v-pre>*</code> 字符，为基于数组的字段使用单个验证消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'custom'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'unique'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Each person must have a unique e-mail address'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义验证规则" tabindex="-1"><a class="header-anchor" href="#自定义验证规则"><span>自定义验证规则</span></a></h2>
<h3 id="使用规则对象" tabindex="-1"><a class="header-anchor" href="#使用规则对象"><span>使用规则对象</span></a></h3>
<p>Laravel 提供了许多有用的验证规则；同时也支持自定义规则。注册自定义验证规则的方法之一，就是使用规则对象。可以使用 Artisan 命令 <code v-pre>make:rule</code> 来生成新的规则对象。接下来，让我们用这个命令生成一个验证字符串是否是大写的规则。Laravel 会将新的规则存放在 <code v-pre>app/Rules</code> 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>rule Uppercase</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦创建了规则，我们就可以定义它的行为。规则对象包含两个方法： <code v-pre>passes</code> 和 <code v-pre>message</code>。<code v-pre>passes</code> 方法接收属性值和名称，并根据属性值是否符合规则而返回 <code v-pre>true</code> 或 <code v-pre>false</code>。 <code v-pre>message</code> 方法应返回验证失败时应使用的验证错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">Rule</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 确定验证规则是否通过。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$attribute</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$value</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">passes</span><span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">strtoupper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token variable">$value</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取验证错误消息。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">message</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'The :attribute must be uppercase.'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然， 如果你希望从翻译文件中返回一个错误消息，你可以从 <code v-pre>message</code> 方法中调用辅助函数 <code v-pre>trans</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取验证错误消息。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">message</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'validation.uppercase'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦规则对象被定义好后，你可以通过将规则对象的实例和其他验证规则一起来传递给验证器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Uppercase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'string'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Uppercase</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用闭包" tabindex="-1"><a class="header-anchor" href="#使用闭包"><span>使用闭包</span></a></h3>
<p>如果你在应用程序中只需要一次自定义规则的功能，则可以使用闭包代替规则对象。闭包接收属性的名称、属性的值、失败回调，如果验证失败需要调用<code v-pre>$fail</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$value</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token operator">.</span><span class="token string single-quoted-string">' is invalid.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用扩展" tabindex="-1"><a class="header-anchor" href="#使用扩展"><span>使用扩展</span></a></h3>
<p>注册自定义的验证规则的另一种方法是使用 <code v-pre>Validator</code> <a href="https://learnku.com/docs/laravel/5.8/facades" target="_blank" rel="noopener noreferrer">facade</a> 中的 <code v-pre>extend</code> 方法。让我们在 <a href="https://learnku.com/docs/laravel/5.8/providers" target="_blank" rel="noopener noreferrer">服务容器</a> 中使用这个方法来注册自定义验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 注册服务提供器。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 引导应用程序。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>自定义的验证闭包接收四个参数：要被验证的属性名称 <code v-pre>$attribute</code>、属性的值 <code v-pre>$value</code>、传入验证规则的参数数组 <code v-pre>$parameters</code> 、以及 <code v-pre>Validator</code> 实列。</p>
<p>除了使用闭包，你也可以传入类和方法到 extend 方法中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'FooValidator@validate'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="定义错误消息" tabindex="-1"><a class="header-anchor" href="#定义错误消息"><span>定义错误消息</span></a></h4>
<p>你还需要为自定义规则定义错误信息。你可以使用内联自定义消息数组或者在验证语言文件中添加条目来实现这一功能。 消息应该被放到数组的第一位， 而不是在只用于存放属性指定错误信息的 <code v-pre>custom</code> 数组内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"foo"</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"Your input was invalid!"</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token string double-quoted-string">"accepted"</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"The :attribute must be accepted."</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 其余的验证错误消息...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当创建一个自定义验证规则时，你可能有时候需要为错误信息定义自定义占位符。可以通过创建自定义验证器然后调用 <code v-pre>Validator</code> 门面上的 <code v-pre>replacer</code> 方法。你可以在 <a href="https://learnku.com/docs/laravel/5.8/providers" target="_blank" rel="noopener noreferrer">服务容器</a> 的 <code v-pre>boot</code> 方法中执行如下操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导应用程序。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">replacer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$rule</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">str_replace</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="隐式扩展" tabindex="-1"><a class="header-anchor" href="#隐式扩展"><span>隐式扩展</span></a></h3>
<p>默认情况下，当所要验证的属性不存在或包含一个空字符串时，使用包含自定义扩展的正常的验证规则是不会执行的。例如，<a href="#rule-unique"><code v-pre>unique</code></a> 规则将不会检验空字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$rules</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:users,name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">passes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果即使属性为空也要验证规则，则一定要暗示属性是必须的。要创建这样一个「隐式」扩展，可以使用 <code v-pre>Validator::extendImplicit()</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">extendImplicit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：「隐式」扩展只<strong>暗示</strong>该属性是必需的。至于它到底是缺失还是空值这取决于你。</p>
</blockquote>
<h4 id="隐式规则对象" tabindex="-1"><a class="header-anchor" href="#隐式规则对象"><span>隐式规则对象</span></a></h4>
<p>如果你想要在属性为空时执行规则对象，你应该实现 <code v-pre>Illuminate\Contracts\Validation\ImplicitRule</code> 接口。这个接口将充当验证器的「标记接口」；因此，它不包含你要实现的任何方法。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/validation/5144" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/val...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/validation/5144" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/val...</a></p>
</blockquote>
</div></template>


